#include <string.h>
#include <iostream>
#include "Input.h"

using namespace JDL;
using namespace std;

Input::Input(SDL_Event *event)
{
  bzero(_key, SDLK_LAST);
  _key_down = false;
  _event = event;
  bzero(_mousebuttons, 8);
}

Input::~Input()
{

}

bool	Input::IsKeyDown(SDLKey key) const
{
  if (_key[key] == true)
    return (true);
  return (false);
}

Uint16	Input::GetMouseX() const
{
  return (_mousex);
}

Uint16	Input::GetMouseY() const
{
  return (_mousey);
}

Uint16	Input::GetMouseXRel() const
{
  return (_mousexrel);
}

Uint16	Input::GetMouseYRel() const
{
  return (_mouseyrel);
}

SDL_Rect	Input::GetMousePos() const
{
  SDL_Rect	rect;

  rect.x = _mousex;
  rect.y = _mousey;
  return (rect);
}

SDL_Rect	Input::GetMousePosRel() const
{
  SDL_Rect	rect;

  rect.x = _mousexrel;
  rect.y = _mouseyrel;
  return (rect);
}

bool		Input::IsMouseDown(Uint8 button) const
{
  if (button >= 0 && button < 8)
    {
      if (_mousebuttons[button] == true)
	return (true);
      return (false);
    }
  return (false);
}

void		Input::Update()
{
  this->Refresh();
}

void		Input::Refresh()
{
  if (_event->type == SDL_KEYDOWN)
    {
      _key[_event->key.keysym.sym] = true;
      _key_down = true;
    }
  else if (_event->type == SDL_KEYUP)
    {
      _key[_event->key.keysym.sym] = false;
      _key_down = false;
    }
  else if (_event->type == SDL_MOUSEMOTION)
    {
      _mousex = _event->motion.x;
      _mousey = _event->motion.y;
      _mousexrel = _event->motion.xrel;
      _mouseyrel = _event->motion.yrel;
    }
  else if (_event->type == SDL_MOUSEBUTTONDOWN)
    _mousebuttons[_event->button.button] = true;
  else if (_event->type == SDL_MOUSEBUTTONUP)
    _mousebuttons[_event->button.button] = false;
}
